import { ViewController } from './ViewController';

/**
 * Action class - Represents a user-executable action like Save, Delete, etc.
 */
export class Action {
  private _id: string;
  private _caption: string;
  private _controller: ViewController | null = null;
  private _enabled: boolean = true;
  private _visible: boolean = true;
  private _imageUrl: string | null = null;
  private _confirmationMessage: string | null = null;
  
  /**
   * Handler function for the action execution
   */
  private _executeHandler: (() => void) | null = null;

  /**
   * Create a new Action
   * @param id Action identifier
   * @param caption Action caption
   */
  constructor(id: string, caption: string) {
    this._id = id;
    this._caption = caption;
  }

  /**
   * Get the action ID
   */
  public get id(): string {
    return this._id;
  }

  /**
   * Get the action caption
   */
  public get caption(): string {
    return this._caption;
  }

  /**
   * Set the action caption
   */
  public set caption(value: string) {
    this._caption = value;
  }

  /**
   * Get the controller this action is registered with
   */
  public get controller(): ViewController | null {
    return this._controller;
  }

  /**
   * Set the controller for this action
   * @param controller The controller to register with
   */
  public setController(controller: ViewController): void {
    this._controller = controller;
  }

  /**
   * Get whether this action is enabled
   */
  public get enabled(): boolean {
    return this._enabled && (this._controller?.active ?? false);
  }

  /**
   * Set whether this action is enabled
   */
  public set enabled(value: boolean) {
    if (this._enabled !== value) {
      this._enabled = value;
    }
  }

  /**
   * Get whether this action is visible
   */
  public get visible(): boolean {
    return this._visible;
  }

  /**
   * Set whether this action is visible
   */
  public set visible(value: boolean) {
    if (this._visible !== value) {
      this._visible = value;
    }
  }

  /**
   * Get the image URL for this action
   */
  public get imageUrl(): string | null {
    return this._imageUrl;
  }

  /**
   * Set the image URL for this action
   */
  public set imageUrl(value: string | null) {
    this._imageUrl = value;
  }

  /**
   * Get the confirmation message for this action
   */
  public get confirmationMessage(): string | null {
    return this._confirmationMessage;
  }

  /**
   * Set the confirmation message for this action
   */
  public set confirmationMessage(value: string | null) {
    this._confirmationMessage = value;
  }

  /**
   * Set the execute handler for this action
   * @param handler The function to execute when this action is triggered
   */
  public setExecuteHandler(handler: () => void): void {
    this._executeHandler = handler;
  }
  /**
   * Execute this action
   */
  public execute(): void {
    console.log(`Action ${this._id}: execute called, enabled=${this.enabled}`);
    if (this.enabled) {
      if (this._confirmationMessage) {
        if (window.confirm(this._confirmationMessage)) {
          this._executeAction();
        }
      } else {
        this._executeAction();
      }
    }
  }

  /**
   * Update the action state
   * Called when controller active state changes
   */
  public updateState(): void {
    // This method is called when the controller active state changes
    // Subclasses can override this to update the action state
    console.log(`Action ${this._id}: updateState called, controller=${this._controller?.id}, active=${this._controller?.active}`);
  }

  /**
   * Execute the action handler
   */
  private _executeAction(): void {
    console.log(`Action ${this._id}: _executeAction called, has handler=${!!this._executeHandler}`);
    if (this._executeHandler) {
      this._executeHandler();
    }
  }
}
